package com.darren.archive.action;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
//import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.darren.framework.action.BaseAction;
import com.darren.framework.entity.SystemParameter;
import com.darren.framework.entity.User;
import com.darren.framework.service.BaseService;
import com.darren.framework.service.SystemParameterService;
import com.darren.framework.service.UserService;
import com.darren.framework.utils.Constant;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Controller
@RequestMapping("/system")
public class SystemAction extends BaseAction{

	private Logger logger = LogManager.getLogger(SystemAction.class);	
	
	@Autowired
	private SystemParameterService systemParameterService;	
	
	@Autowired
	private UserService userService;
	
	@Autowired
    private CacheManager cacheManager;
	
	@Autowired
	private BaseService baseService;
	
	@Autowired
    private JedisPool jedisPool;//注入JedisPool
	
	@ResponseBody
	@RequestMapping("/test")
	public  Map<String,Object> getDataMap(HttpServletRequest request) throws SQLException{
		Map<String,Object> result = baseService.getDataMap("select * from d_system_parameter; ",new Object[0]);		
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/set")
	public  String jedisSet(HttpServletRequest request) throws SQLException{
		Jedis jedis = jedisPool.getResource();		
		jedis.set("darren", "zhou" + System.currentTimeMillis());		
		return "set";
	}
	
	@ResponseBody
	@RequestMapping("/get")
	public  String jedisGet(HttpServletRequest request) throws SQLException{
		Jedis jedis = jedisPool.getResource();		
		String value = jedis.get("darren")	;
		return "get value:"+value;
	}
	


	
	
	@RequestMapping("/error")
	public String showSelectProduct(HttpServletRequest request){
        logger.info("Hello World!");
		return "/system/error";
	}
	
	@ResponseBody
	@RequestMapping("/parameters2")
	public  List<SystemParameter> getAllParameters(HttpServletRequest request){
		List<SystemParameter> parameters = systemParameterService.getAll();
		return parameters;
	}
	
	@ResponseBody
	@RequestMapping("/users")
	public  List<User> getAllUsers(HttpServletRequest request){
		List<User> users = userService.getAll();
		return users;
	}
	
	@ResponseBody
	@RequestMapping("/user/delete/{id}")
	public  JSONObject deleteUser(@PathVariable(name="id",required=true) long  id,HttpServletRequest request){
		JSONObject responseJson = new JSONObject();	
		User user = userService.findById(id);
		if(user != null){
			user = userService.delete(user);
		}		
		responseJson.put("status", 0);
		responseJson.put("message", "删除成功！");
		return responseJson;
	}
	
	
	@ResponseBody
	@RequestMapping("/parameter/{id}")
	public  JSONObject getSystemParameter(@PathVariable(name="id",required=true) Long  id,HttpServletRequest request){
		JSONObject responseJson = new JSONObject();	
		Element element = cacheManager.getCache("systemPparameter").get(id);
		responseJson.put("message", "未正常获取！");
		SystemParameter paramter = null;
		if(element != null){
			paramter = (SystemParameter) element.getObjectValue();
			responseJson.put("message", "缓存获取！");
		}else{
			paramter = systemParameterService.findById(id);
			responseJson.put("message", "数据库获取！");
		}
		responseJson.put("status", 0);
		responseJson.put("data", paramter);
		return responseJson;
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/parameters")
	public  JSONObject getSystemParameters(HttpServletRequest request){
		JSONObject responseJson = new JSONObject();	
		Element element = cacheManager.getCache(Constant.CACHE_SYSTEM_PARAMS).get("allSystemParameter");
		responseJson.put("message", "未正常获取！");
		List<SystemParameter> paramters = null;
		if(element != null){
			paramters = (List<SystemParameter>)element.getObjectValue();
			responseJson.put("message", "缓存获取！");
		}else{
			paramters = systemParameterService.getAllSystemParameter();
			element = new Element("allSystemParameter",paramters);
			cacheManager.getCache(Constant.CACHE_SYSTEM_PARAMS).put(element);
			responseJson.put("message", "数据库获取！");
		}
		responseJson.put("status", 0);
		responseJson.put("data", paramters);
		return responseJson;
	}
	
	
	@ResponseBody
	@RequestMapping("/add")
	public  JSONObject saveSystemParameters(HttpServletRequest request) throws Exception{
		JSONObject responseJson = new JSONObject();	
		SystemParameter parameter = new SystemParameter();
		parameter.setLookupCat("123");
		parameter.setLookupType("456");
		parameter.setLookupValue("789");
		parameter = systemParameterService.save(parameter);
		responseJson.put("status", 0);
		responseJson.put("data", parameter);
		return responseJson;
	}
	
	
	@ResponseBody
	@RequestMapping("/tomcat")
	public  String getTomcatInfo(HttpServletRequest request){
		 String tomcat_path = System.getProperty( "catalina.home" );  
         System.out.println(tomcat_path);  
         //获取Tomcat服务器所在路径的最后一个文件目录  
         String bin_path = tomcat_path.substring(tomcat_path.lastIndexOf("\\")+1,tomcat_path.length());  
         System.out.println(bin_path);  
         //若最后一个文件目录为bin目录，则服务器为手动启动  
		
		return "";
	}
	
	
}
